<template>
<!--  <div>-->
  <div class="product-view">
    <div class="header">
        <h1>Product View</h1>
        <h2>{{ msg }}</h2>

    </div>
    <!-- 搜索框 -->
    <div class="search-container">
      <!-- 新增的包裹容器 -->
      <div class="search-wrapper">
        <input type="text" placeholder="请输入商品名称搜索..." v-model="keyword">
        <button @click="searchProduct">
          <i class="fas fa-search"></i> 搜索 <!-- 新增搜索图标 -->
        </button>
      </div>
    </div>
    <button class="add-btn"  @click="updateAddFlag = true">新增商品信息</button>
<!--        <div>-->
<!--          <button @click="getAllProducts" class="action-button">查询所有商品</button>-->
<!--&lt;!&ndash;&lt;!&ndash;        添加商品信息 &ndash;&gt;&ndash;&gt;-->
<!--&lt;!&ndash;          <button&ndash;&gt;-->

<!--        </div>-->
      <!--            我的商品信息-->
      <div class="table-container">
        <table>
          <!--表头-->
          <thead>
          <tr>
            <th>商品编号</th>
            <th>商品名称</th>
            <th>商品描述</th>
            <th>商品图片</th>
            <th>商品价格</th>
            <th>商品库存</th>
            <th>商品分类</th>
            <th>商品状态</th>
            <th>操作</th>
          </tr>
          </thead>




          <!--表格内容-->
          <tbody>
          <tr v-for="(productvo,index) in products" :key="index">
            <td>{{ productvo.product.productId }}</td>
            <td>{{ productvo.product.productName }}</td>
            <td>{{ productvo.product.description }}</td>
<!--            <td>{{ productvo.product.image }}</td>-->
            <td><img :src="productvo.product.image" alt="商品图片" width="100px" height="100px"></td>
            <td>{{ productvo.product.price }}</td>
            <td>{{ productvo.product.stock }}</td>
            <td>{{ productvo.categoryName }}</td>
            <td>{{ productvo.product.status }}</td>
<!--            <td>{{ productvo.product.created_at }}</td>-->
<!--            <td>{{ productvo.product.updated_at }}</td>-->


            <td>
              <button @click="editProducts(productvo.product)">编辑</button> |
              <button @click="deleteProducts(productvo.product.productId)">删除</button> |
              <button @click="addCart(productvo.product.productId)">加入购物车</button>
            </td>
            </tr>
          </tbody>
        </table>
      </div>
<!--        <button class="add-btn"  @click="updateAddFlag = true">新增商品信息</button>-->
      <div v-if="updateAddFlag" class="modal-overlay">
        <div>
          <h2>新增商品信息</h2>
          <span>商品名称:</span>
          <input type="text" v-model="updateAddProduct.productName">
          <span>商品描述:</span>
          <input type="text" v-model="updateAddProduct.description">
          <span>商品图片:</span>
          <input type="text" v-model="updateAddProduct.image">
          <span>商品价格:</span>
          <input type="text" v-model="updateAddProduct.price">
          <span>商品库存:</span>
          <input type="text" v-model="updateAddProduct.stock">
          <span>商品分类:</span>
          <select v-model="updateAddProduct.categoryId">
            <option v-for="category in categories" :key="category.categoryId" :value="category.categoryId">{{ category.categoryName }}</option>
          </select>
          <span>商品状态:</span>
          <select v-model="updateAddProduct.status">
            <option value="1">上架</option>
            <option value="0">下架</option>
          </select>
          <div class="button-group">
          <button @click="saveProduct">保存</button>
          <button @click="updateAddFlag = false">取消</button>
          </div>
        </div>
      </div>

<!--      编辑商品信息-->
      <div v-if="editFlag" class="modal-overlay">
        <div class="modal">
          <div class="modal-content">
          <h2>编辑商品信息</h2>
          <span>商品名称:</span>
          <input type="text" v-model="editProduct.productName">
          <span>商品描述:</span>
          <input type="text" v-model="editProduct.description">
          <span>商品图片:</span>
          <input type="text" v-model="editProduct.image">
          <span>商品价格:</span>
          <input type="text" v-model="editProduct.price">
          <span>商品库存:</span>
          <input type="text" v-model="editProduct.stock">
          <span>商品分类:</span>
          <select v-model="editProduct.categoryId">
            <option v-for="category in categories" :key="category.categoryId" :value="category.categoryId">{{ category.categoryName }}</option>
          </select>
          <span>商品状态:</span>
          <select v-model="editProduct.status">
            <option value=1>上架</option>
            <option value=0>下架</option>
          </select>
          <button @click="saveEditProduct">保存</button>
          <button @click="editFlag = false">取消</button>
      </div>
      </div>
    </div>

<!--    <img src="../../assets/logo.png"/>-->
<!--    <img :src="testImg"/>-->

  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "ProductView",
  data() {
    return {
      msg: "商品信息页面",
      productName: "",// 商品名称
      products: [],// 商品列表
      editFlag: false,// 编辑状态
      editProduct: {},// 编辑的商品信息
      // 新增上架，下架状态
      categories: [],// 商品分类列表
      updateAddFlag: false,// 更新新增状态
      updateAddProduct: {},// 更新新增后的商品信息
      keyword: "",// 搜索关键字
      // testImg: '../../assets/logo.png'
      testImg: "../../assets/logo.png"
    };
  },
  methods: {
    // 查询所有商品
    getAllProducts() {
      axios.get("/api/product/all").then(response => {
        if(response.data){
          if(response.status===200)  this.products = response.data;
          else alert("哥们手速太快了,服务器还没准备好,请稍后再试!")
        }
        console.log(this.products);
      }).catch(error => {
            console.log("获取商品列表失败,原因是:");

            console.log(error);
            alert("获取商品列表失败,原因是:" + error);
      });
  },
    // 搜索商品
    searchProduct() {
      axios.get("/api/product/search/" + this.keyword).then(response => {
        if (response.status === 200) {
          console.log("response.data");
          this.products = response.data;
        }
        console.log(this.products);
      }).catch(error => {
        console.log("搜索商品失败,原因是:");
        console.log(error);
        alert("搜索商品失败,原因是:" + error);
      });
    },
    // 编辑商品
    editProducts(product) {
      this.editProduct = product;
      this.editFlag = true;
    },
    // 保存编辑的商品信息
    saveEditProduct() {
      axios.put("/api/product/update", this.editProduct).then(response => {
        if(response.status === 200){
          this.editFlag = false;
          alert("编辑成功!");
          this.getAllProducts();
        }
      }).catch(error => {
        console.log("编辑商品失败,原因是:");
        console.log(error);
        alert("编辑商品失败,原因是:" + error);
      });
    },
    // 删除商品
    deleteProducts(id) {
      axios.delete("/api/product/delete/" + id).then(response => {
        if(response.status === 200){
          alert("删除成功!");
          this.getAllProducts();
        }
      }).catch(error => {
        console.log("删除商品失败,原因是:");
        console.log(error);
        alert("删除商品失败,原因是:" + error);
      });
    },
    // 新增商品
    saveProduct() {
      axios.post("/api/product/add", this.updateAddProduct).then(response => {
        if (response.status === 200) {
          alert("新增成功!");
          this.updateAddFlag = false;
          this.getAllProducts();
        } else {
          alert("新增失败!");
        }
      }).catch(error => {
        console.log("新增商品失败,原因是:");
        console.log(error);
        alert("新增商品失败,原因是:" + error);
      });
      },
    // 加入购物车
    addCart(productId) {
      axios.post("/api/cart/add/" + productId).then(response => {
        if(response.status === 200){
          alert("加入购物车成功!");
        }
      }).catch(error => {
        console.log("加入购物车失败,原因是:");
        console.log(error);
        alert("加入购物车失败,原因是:" + error);
      });
    },
    // 获取所有商品分类
    getAllCategories() {
      axios.get("/api/category/all").then(response => {
        if(response.status === 200){
          this.categories = response.data;
        }
        console.log(this.categories);
      }).catch(error => {
        console.log("获取商品分类失败,原因是:");
        console.log(error);
        alert("获取商品分类失败,原因是:" + error);
      });
    }

  },
  mounted() {
    import('../../assets/logo.png').then(res => {
      this.testImg = res.default
    })
    console.log("ProductView mounted");
    this.getAllProducts();
    this.getAllCategories();
  }
}
</script>
<style>
  /* 基础样式 */
body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  min-height: 100vh;
  margin: 0;
  padding: 20px;
  color: #333;
  line-height: 1.6;
}
  /* 新增的搜索容器样式 */
  .search-container {
    padding: 30px 20px;
    background: #f8fafc;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid #e9ecef;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.03);
  }

  /* 新增的搜索框包裹容器 */
  .search-wrapper {
    display: flex;
    width: 100%;
    max-width: 700px;
    height: 60px;
    box-shadow: 0 8px 20px rgba(52, 152, 219, 0.2);
    border-radius: 50px;
    overflow: hidden;
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  }

  .search-wrapper:focus-within {
    box-shadow: 0 10px 30px rgba(155, 89, 182, 0.3);
    transform: translateY(-2px);
  }

  .search-wrapper input {
    flex-grow: 1;
    border: none;
    padding: 0 25px;
    font-size: 1.1rem;
    outline: none;
    background: white;
    transition: all 0.3s ease;
  }

  .search-wrapper input::placeholder {
    color: #a0aec0;
    font-weight: 400;
  }

  .search-wrapper input:focus {
    background: #f8fafc;
  }

  .search-wrapper button {
    background: linear-gradient(to right, #3498db, #9b59b6);
    border: none;
    color: white;
    padding: 0 35px;
    font-size: 1.1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .search-wrapper button:hover {
    background: linear-gradient(to right, #2980b9, #8e44ad);
    padding: 0 40px;
  }

  .search-wrapper button:active {
    transform: scale(0.98);
  }

  /* 响应式调整 */
  @media (max-width: 768px) {
    .search-container {
      padding: 20px 15px;
    }

    .search-wrapper {
      height: 50px;
    }

    .search-wrapper input {
      padding: 0 20px;
      font-size: 1rem;
    }

    .search-wrapper button {
      padding: 0 25px;
      font-size: 1rem;
    }
  }
  .modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
  }

  .modal {
    background: white;
    border-radius: 16px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.25);
    width: 90%;
    max-width: 600px;
    max-height: 90vh;
    overflow-y: auto;
    transform: translateY(0);
    transition: transform 0.3s ease;
  }
/* 头部样式 */
div > div:first-child {
  text-align: center;
  padding: 30px 0 20px;
}

h1 {
  font-size: 2.5rem;
  color: #2c3e50;
  margin-bottom: 10px;
  position: relative;
  display: inline-block;
}

h1::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 4px;
  background: linear-gradient(to right, #3498db, #9b59b6);
  border-radius: 2px;
}

h2 {
  font-size: 1.5rem;
  color: #7f8c8d;
  font-weight: 400;
}

/* 表格容器 */
div > div:nth-child(2) > div:first-child {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  margin: 0 auto 30px;
  max-width: 1400px;
  padding: 20px;
  overflow-x: auto;
}

/* 表格样式 */
table {
  width: 100%;
  border-collapse: collapse;
  min-width: 1000px;
}

thead {
  background: linear-gradient(to right, #3498db, #9b59b6);
  color: white;
}

th {
  padding: 16px 15px;
  text-align: left;
  font-weight: 500;
  font-size: 1.05rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

tbody tr {
  border-bottom: 1px solid #eee;
  transition: background 0.2s ease;
}

tbody tr:hover {
  background-color: #f8fafc;
}

td {
  padding: 14px 15px;
  color: #2c3e50;
}

/* 状态标签 */
.status-active {
  background: #e8f6ef;
  color: #27ae60;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
  display: inline-block;
}

.status-inactive {
  background: #fdecea;
  color: #e74c3c;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
  display: inline-block;
}

/* 操作按钮 */
td button {
  padding: 8px 15px;
  border-radius: 6px;
  border: none;
  cursor: pointer;
  transition: all 0.2s ease;
  font-weight: 500;
  margin: 0 5px;
}

td button:first-child {
  background: #3498db;
  color: white;
}

td button:nth-child(2) {
  background: #e74c3c;
  color: white;
}

td button:last-child {
  background: #2ecc71;
  color: white;
}

td button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* 新增商品按钮 */
button {
  display: block;
  margin: 0 auto 30px;
  padding: 12px 28px;
  background: linear-gradient(to right, #3498db, #2980b9);
  color: white;
  border: none;
  border-radius: 8px;
  font-weight: 500;
  font-size: 1.1rem;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
}

button:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 16px rgba(52, 152, 219, 0.4);
}

/* 模态框样式 */
div[v-if] {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

div[v-if].active {
  opacity: 1;
  pointer-events: all;
}

div[v-if] > div {
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.25);
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  transform: translateY(20px);
  transition: transform 0.3s ease;
}

div[v-if].active > div {
  transform: translateY(0);
}

/* 模态框内容 */
div[v-if] > div > div {
  padding: 30px;
}

div[v-if] h2 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 25px;
  font-weight: 600;
  font-size: 1.8rem;
  position: relative;
  padding-bottom: 15px;
}

div[v-if] h2::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: linear-gradient(to right, #3498db, #9b59b6);
  border-radius: 2px;
}

/* 表单样式 */
div[v-if] span {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #2c3e50;
}

div[v-if] input,
div[v-if] select {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 1rem;
  margin-bottom: 20px;
  transition: all 0.3s ease;
}

div[v-if] input:focus,
div[v-if] select:focus {
  border-color: #3498db;
  outline: none;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

/* 按钮组 */
.button-group {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 20px;
}

.button-group button {
  padding: 12px 25px;
  border-radius: 8px;
  border: none;
  font-weight: 500;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s ease;
  margin: 0;
  width: auto;
}

.button-group button:first-child {
  background: #2ecc71;
  box-shadow: 0 4px 12px rgba(46, 204, 113, 0.3);
}

.button-group button:last-child {
  background: #95a5a6;
  box-shadow: 0 4px 12px rgba(149, 165, 166, 0.3);
}

.button-group button:hover {
  transform: translateY(-2px);
}


/* 响应式调整 */
@media (max-width: 768px) {
  div[v-if] > div {
    width: 95%;
    padding: 15px;
  }

  div[v-if] > div > div {
    padding: 20px;
  }

  table {
    min-width: 700px;
  }

  h1 {
    font-size: 2rem;
  }

  h2 {
    font-size: 1.3rem;
  }


  /* 悬浮模态框容器 */
  div[v-if] {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
  }

  /* 激活状态 */
  div[v-if].active {
    opacity: 1;
    pointer-events: all;
  }

  /* 悬浮框内容 */
  div[v-if] > div {
    background: white;
    border-radius: 16px;
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.25);
    width: 90%;
    max-width: 700px;
    max-height: 90vh;
    overflow-y: auto;
    transform: translateY(20px);
    transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  }

  /* 激活状态的悬浮框动画 */
  div[v-if].active > div {
    transform: translateY(0);
  }

  /* 输入框样式 */
  div[v-if] input,
  div[v-if] select {
    width: 100%;
    padding: 14px 18px;
    border: 1px solid #ddd;
    border-radius: 10px;
    font-size: 1.05rem;
    margin-bottom: 20px;
    transition: all 0.3s ease;
  }

  /* 输入框聚焦效果 */
  div[v-if] input:focus,
  div[v-if] select:focus {
    border-color: #3498db;
    outline: none;
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
  }
}


</style>
